﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FileMover.SharedControls
{
    public partial class ConfigControl : UserControl
    {
        private FileMover.Service.Model _model;
        public FileMover.Service.Model model
        {
            get { return _model; }
            set
            {
                if (_model == value)
                    return;
                _model = value;
                ModelChanged();
            }
        }

        public ConfigControl()
        {
            InitializeComponent();
        }

        private bool _loaded = false;

        protected void ModelChanged()
        {
            if (!_loaded)
                return;

            if (model == null)
            {
                UpdateSelectedDirectory(null);
                lbDirectories.DataSource = null;
                return;
            }

            lbDirectories.DataSource = model.Directories;
        }

        private void ConfigControl_Load(object sender, EventArgs e)
        {
            _loaded = true;

            ModelChanged();
        }

        private void lbDirectories_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = (e.ListItem as FileMover.Service.DirectoryItemProvider).SourceDirectory;
        }

        private FileMover.Service.DirectoryItemProvider _selectedDirectory;
        private FileMover.Service.Rule _selectedRule;
        private void UpdateSelectedDirectory(FileMover.Service.DirectoryItemProvider selectedDirectory)
        {
            if (selectedDirectory == _selectedDirectory)
                return;

            _selectedDirectory = selectedDirectory;
            if (_selectedDirectory == null)
            {
                UpdateSelectedRule(null);
                lbRules.DataSource = null;
                tbSourceDir.Text = string.Empty;
                tbSourceDir.Enabled = false;
                btnSourceDir.Enabled = false;

                btnAddRule.Enabled = false;
                return;
            }

            tbSourceDir.Text = _selectedDirectory.SourceDirectory;
            tbSourceDir.Enabled = true;
            btnSourceDir.Enabled = true;

            lbRules.DataSource = _selectedDirectory.Rules;
            btnAddRule.Enabled = true;
        }
        private void UpdateSelectedRule(FileMover.Service.Rule rule)
        {
            if (_selectedRule == rule) return;
            _selectedRule = rule;

            if (_selectedRule == null)
            {
                UpdateSelectedAction(null);
                lbActions.DataSource = null;
            }
            else
            {
                lbActions.DataSource = _selectedRule.FiltersAndActions;
            }
        }

        private FileMover.Service.IRuleItem _selectedRuleItem;
        
        private void UpdateSelectedAction(FileMover.Service.IRuleItem item)
        {
            if (item == _selectedRuleItem)
                return;

            _selectedRuleItem = item; ;
            if (_selectedRuleItem == null)
            {
                propertyGrid1.SelectedObject = null;
            }
            else
            {
                propertyGrid1.SelectedObject = _selectedRuleItem;
            }
        }

        private void lbDirectories_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateSelectedDirectory(lbDirectories.SelectedItem as FileMover.Service.DirectoryItemProvider);
        }

        private void lbRules_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateSelectedRule(lbRules.SelectedItem as FileMover.Service.Rule);
        }

        private void lbRules_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = (e.ListItem as FileMover.Service.Rule).Comment;
        }

        private void lbActions_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateSelectedAction(lbActions.SelectedItem as FileMover.Service.IRuleItem);
        }

        private void lbActions_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = (e.ListItem as FileMover.Service.IRuleItem).DisplayString();
        }

        class StringBufferLog : FileMover.Service.ILog
        {
            StringBuilder _buffer;
            public StringBufferLog(StringBuilder buffer)
            {
                _buffer = buffer;
            }

            #region ILog Member

            public void Log(FileMover.Service.LevelEnum level, string format, params object[] formatParams)
            {
                _buffer.AppendFormat("[{0}] ", level.ToString());
                _buffer.AppendFormat(format, formatParams);
                _buffer.AppendLine();
            }

            #endregion
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            var form = new VisibleLogForm();
            form.Show(this);
            //var buffer = new StringBuilder();
            //var log = new StringBufferLog(buffer);
            model.Test(form.GetLogger());


        }

        private void btnAddDirectory_Click(object sender, EventArgs e)
        {

        }

        private void btnAddAction_Click(object sender, EventArgs e)
        {
            if(_selectedRule==null)
                return;

            var form = new AddActionForm();
            if (form.ShowDialog(this) != DialogResult.OK)
                return;

            var factory = form.SelectedFactory;
            if (factory == null)
                return;

            var filterOrAction = _selectedRule.AddFilterOrAction(factory);
            lbActions.DataSource = null;
            lbActions.DataSource = _selectedRule.FiltersAndActions;
            lbActions.SelectedItem = filterOrAction;
            //_selectedRule.
        }
    }
}
